//
//  YCHeros.m
//  LOLBox
//
//  Created by bb on 15-3-29.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCHeros.h"
#import "HttpRequestManager.h"
#import "YCHeroModel.h"
#import "YCHeroCell.h"
#import "YCAppDelegate.h"
#import "DataBaseSImple.h"

#import "YCHeroDetail.h"

#define kHerosFree @"http://lol.data.shiwan.com/lolHeros/"
#define kHerosAll @"http://lol.data.shiwan.com/lolHeros/"
//  ?filter=all&type=all   //pra..

@interface YCHeros ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic ,strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation YCHeros

- (void)viewWillAppear:(BOOL)animated
{
    animated = YES;
    YCAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate showTabBar];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self createNavTitleView:@"英雄"];
        [self createRefreshBarBtn];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createNavView];
    
    NSLog(@"%@",NSHomeDirectory());
    
    [self getHerosFree:@"herosfree"];
    [self createCollectionView];
}

//视图切换
- (void)createNavView
{
    NSArray *nameArr = @[@"收藏英雄",@"免费英雄",@"所有英雄"];
    NSArray *urlArr = @[kHerosAll,
                        kHerosFree,
                        kHerosAll];
    PublicNavView *navView = [[PublicNavView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    //navView.backgroundColor = [UIColor blueColor];
    // 第一步，把数组传过去
    [navView senderValue:nameArr andSecond:urlArr];
    //第二步，创建按钮
    [navView creatTheBtnAndIndex:1];
    //第三步，通过BLOCK回调得到网址
    [navView changValue:@"1" senderBlock:^(NSString *str,NSInteger index) {
        NSLog(@"Pathstr :%@",str);
        if (self.dataSource.count > 0)
        {
            [self.dataSource removeAllObjects];
        }

        
        if (index == 1)
        {
            
            [self getHerosFree:@"herosfree"];
        }
        
        else if (index == 2)
        {
            
            [self AFN:@"herosall"];
        }
        else if (index == 0)
        {
            self.dataSource = [[DataBaseSimple shareInstance] selectFromDBWithAllFavHero];
            [self.collectionView reloadData];
        }
        
    }];
    
    [self.view addSubview:navView];
}

//数据请求 所有
- (void)AFN:(NSString *)path
{
    [[HttpRequestManager shareInstance] getLocalHerosInfo:path withSuccess:^(NSMutableArray *arr) {
        NSLog(@"2");
        self.dataSource = arr;
    }];
    
    [self.collectionView reloadData];
}

- (void)getHerosFree:(NSString *)str
{

    [[HttpRequestManager shareInstance] getLocalFreeHerosInfo:str withSuccess:^(NSMutableArray *arr) {
        NSLog(@"11");
        self.dataSource = arr;
        
        [self.collectionView reloadData];
    }];
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30, CGRectGetWidth([[UIScreen mainScreen] bounds]), CGRectGetHeight([[UIScreen mainScreen] bounds]) - 50 - 64 - 30) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    //注册Cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"YCHeroCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];

    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YCHeroCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    YCHeroModel *model = _dataSource[indexPath.row];
    
    [cell fillCellWithModel:model];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"选择这里");
    
    YCHeroDetail *detail = [[YCHeroDetail alloc] initWithNibName:@"YCHeroDetail" bundle:nil];
    YCHeroModel *m = _dataSource[indexPath.row];
    [detail senderModel:m];
    
    [self.navigationController pushViewController:detail animated:YES];
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(70, 100);
}
//header And Footer
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    YCHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
//    
//    NSString *title = _headerArr[indexPath.section];
//    
//    [headerView initTitle:title];
//    return headerView;
//    
//}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    CGFloat lengFloat = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    CGFloat spLeng = (lengFloat - 70 * 4) / 8;
    return  UIEdgeInsetsMake(5, spLeng, 5, spLeng);
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
